$(function () {
    let t_first = $('#temp-menu').children().first();
    t_first.addClass('active selected');
    let templateId = t_first.attr('data-value');
    let default_text = $('div.default.text');
    let text = t_first.find('span').text()

    let template_input = $('#templateId');
    default_text.attr("data-value", templateId);
    default_text.html(text);

    let flag = true;

    template_input.val(text);
    if (templateId !== undefined) {
        load_last_modify_record(templateId);
    }

    template_input.change(function () {
        if (flag) {
            load_last_modify_record($(this).val());
        } else {
            load_modify_record($(this).val())
        }
    });

    $('#all-modify-record').click(function () {
        templateId = $('div.active.selected').attr('data-value');
        if (templateId !== undefined) {
            load_modify_record(templateId);
            flag = false;
        } else {
            failure("没有模板可选");
        }
    });

    $('#last-modify-record').click(function () {
        templateId = $('div.active.selected').attr('data-value');
        if (templateId !== undefined) {
            load_last_modify_record(templateId);
            flag = true;
        } else {
            failure("没有模板可选");
        }
    });

})

function load_last_modify_record(templateId) {
    let workOrderNumber = $('#workOrderNumber').val();
    $.get({
        url: '/produce/viewTheLastOperationRecord/' + workOrderNumber + "/" + templateId,
        success: function (result) {
            switch (result.status) {
                case 200:
                    pack_last_modify_data(result.data);
            }
        }
    })
}

function load_modify_record(templateId) {
    let workOrderNumber = $('#workOrderNumber').val();
    $.get({
        url: '/craft/viewTestDataModificationRecords/' + workOrderNumber + "/" + templateId,
        success: function (result) {
            switch (result.status) {
                case 200:
                    pack_modify_data(result.data);
            }
        }
    })
}

function pack_last_modify_data(data) {
    let thead = $('thead');
    let tbody = $('tbody');

    let head = "<tr>";
    $.each(data.templateNames, function (index, item) {
        head += "<td>" + item + "</td>";
    });
    head += "</tr>";
    thead.html(head);

    let data1 = data.resultList;

    let data2 = data.tips;

    let body;
    let re = "<tr class='blank-line'></tr>";

    for (let i = 0; i < data1.length; i++) {
        body = "<tr>";
        for (let j = 0; j < data1[i].length; j++) {
            body += "<td data-position='top center' data-tooltip='" + data2[i][j] + "'>" + data1[i][j] + "</td>";
        }
        body += "</tr>";
        re += body;
    }
    tbody.html(re);
    two_way_binding();
}

function pack_modify_data(data) {
    let thead = $('thead');
    let tbody = $('tbody');

    let head = "<tr>";
    $.each(data.templateNames, function (index, item) {
        head += "<th>" + item + "</th>";
    });
    thead.html(head + "</tr>");

    let body;
    let re = "";
    $.each(data.resultList, function (index, list) {
        body = "<tr>";
        $.each(list, function (index, item) {
            body += "<td>" + item + "</td>";

        });
        body += "</tr>";
        re += body;
    });
    tbody.html(re);
    two_way_binding();
}

function two_way_binding() {
    let td = $('tbody td');
    let input = $('#content');
    let cur;
    td.click(function () {
        cur = $(this);
        input.val(cur.text());
        cur.keyup(function () {
            input.val(cur.text());
        });
        input.keyup(function () {
            cur.text($(this).val());
        });
    });
}


$('#export-modify-record').click(function () {
    if ($('thead').children().length === 0) {
        failure("请选择模板")
        return;
    }
    $("#myTable").table2excel({
        // 不被导出的表格行的CSS class类
        exclude: ".blank-line",
        // 导出的Excel文档的名称
        name: $('#workOrderNumber').val() + "-修改记录-" + new Date().getTime(),
        // Excel文件的名称
        filename: $('#workOrderNumber').val() + "-修改记录-" + new Date().getTime(),
        bootstrap: false
    });
})